package com.cxlin.cwanandroid.ui.home.viewmodel

import androidx.lifecycle.viewModelScope
import com.cxlin.base.BaseViewModel
import com.cxlin.cwanandroid.model.repository.HomeRepository
import kotlinx.coroutines.launch
import com.cxlin.base.model.api.Result
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.ArticleList
import com.cxlin.cwanandroid.model.bean.Banner
import kotlinx.coroutines.flow.update

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/26 14:08
 */
class HomeViewModel(
    private val homeRepository: HomeRepository
) : BaseViewModel() {

    sealed class ArticleUiState : UiState {
        object Init : ArticleUiState()
        data class IsRefresh(val isRefresh: Boolean) : ArticleUiState()
        data class FetchArticlesSuccess(val articles: ArticleList?) : ArticleUiState()
        data class FetchBannersSuccess(val banners: List<Banner>) : ArticleUiState()
    }

    sealed class ArticleEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : ArticleEvent()
    }

    sealed class ArticleUiIntent : UiIntent {
        object GetBanner : ArticleUiIntent()
        data class GetArticleList(val isRefresh: Boolean = false) : ArticleUiIntent()
        data class CollectArticle(val articleId: Int, val boolean: Boolean) : ArticleUiIntent()
    }

    private var currentPage = 0

    override fun handleIntent(intent: UiIntent) {
        when (intent) {
            is ArticleUiIntent.GetBanner -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        val data = homeRepository.getBanners()
                        if (data is Result.Success) {
                            mUiStateFlow.update {
                                ArticleUiState.FetchBannersSuccess(data.data)
                            }
                        }
                    }

                }
            }

            is ArticleUiIntent.GetArticleList -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            currentPage = 0
                            mUiStateFlow.update {
                                ArticleUiState.IsRefresh(true)
                            }
                        }
                        val result = homeRepository.getArticleList(currentPage)
                        if (result is Result.Success) {
                            mUiStateFlow.update {
                                ArticleUiState.IsRefresh(false)
                            }
                            mUiStateFlow.update {
                                ArticleUiState.FetchArticlesSuccess(result.data)
                            }
                            currentPage += 1
                        } else if (result is Result.Error) {
                            mUiStateFlow.update {
                                ArticleUiState.IsRefresh(false)
                            }
                            mEffectFlow.emit(
                                ArticleEvent.ToastEvent(
                                    result.exception.message ?: "网络异常", true
                                )
                            )

                        }
                    }
                }
            }

            is ArticleUiIntent.CollectArticle -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.boolean) {
                            homeRepository.collectArticle(intent.articleId)
                        } else {
                            homeRepository.cancelCollectArticle(intent.articleId)
                        }

                    }
                }
            }
        }
    }

    override fun createInitState(): UiState {
        return ArticleUiState.Init
    }


}